package com.yupi.usercenter.controller;

import com.alibaba.fastjson.JSON;
import com.yupi.usercenter.Job.Task;
import com.yupi.usercenter.common.BaseResponse;
import com.yupi.usercenter.common.ResultUtils;
import com.yupi.usercenter.model.domain.Cpuresource;
import com.yupi.usercenter.model.domain.Monitor;
import com.yupi.usercenter.service.MonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api
@Slf4j
@RequestMapping("/resource")
public class ResourceController {
    @Resource
    private MonitorService monitorService;
    @Resource
    private Task cpuMonitor;

    @ApiOperation(value = "资源监控")
    @PostMapping("/resourceMonitor")
    public BaseResponse<Monitor> resourceMonitor() {
        return ResultUtils.success(monitorService.resourceMonitor());
    }

    @ApiOperation(value = "CPU监控")
    @PostMapping("/CPUMonitor")
    public void CPUMonitor(Boolean status) {
        log.info("CPU监控状态:{}", status ? "已开启" : "已关闭");
        cpuMonitor.setTasksEnabled(status);
    }

    @ApiOperation(value = "获取CPU占用情况图表")
    @PostMapping("/getCPUChart")
    public BaseResponse<List<Cpuresource>> getCPUChart() {
        return ResultUtils.success(monitorService.getCPUState());

    }
}
